<?php

/**
* Front Controller
*/
class Front_Controller
{

private static $_instance = null;   
private $get = array() ;  
private $view;  // obj 


    private function __construct()
    {
        $this->get = Validator::getInstance()->validateRequest($_REQUEST);
        $this->view = new View();
    } 
      
    /**
    * Запрет на клонирование
    * 
    */
    private function __clone() {}
       
    
    /**
    * Метод для вызова данного класса как singletone
    * 
    */
    public static function getInstance()
    {
         if( null == self::$_instance) { 
            self::$_instance = new Front_Controller();
         }
         return self::$_instance;
    }
  
    
    /**
    * RUN !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    * 
    */
    
    public function run() 
    {
        $size = sizeof($this->get);
        if($size < 1 || empty($this->get['route'])) {     
            $actionController = Action_Controller_Fabric::getController('Index', $this->view, $this->get);
        } else if ($this->get['route'] == 'user/'   &&  !empty($this->get['id']) && $this->get['id'] > 0 ) {
             $actionController = Action_Controller_Fabric::getController('User', $this->view, $this->get);
        } else if ($this->get['route'] == 'message/'   &&  !empty($this->get['id']) && $this->get['id'] > 0 ) {
             $actionController = Action_Controller_Fabric::getController('Message', $this->view, $this->get);
        }  else {    
            switch ($this->get['route']) {
            case 'index': $actionController = Action_Controller_Fabric::getController('Index', $this->view, $this->get);
            break;
            case 'registration/': $actionController = Action_Controller_Fabric::getController('Registration', $this->view ,$this->get);
            break;
            case 'login/': $actionController = Action_Controller_Fabric::getController('Login', $this->view ,$this->get);        
            break;
            case 'personal/': $actionController = Action_Controller_Fabric::getController('Personal', $this->view, $this->get);
            break;
            case 'logout/' : $actionController = Action_Controller_Fabric::getController('Logout', $this->view, $this->get);
            break;
            case 'search/' : $actionController = Action_Controller_Fabric::getController('Search', $this->view, $this->get);
            break;
            case 'messages/' : $actionController = Action_Controller_Fabric::getController('Messages', $this->view, $this->get);
            break;
            case 'admin/': $actionController = Action_Controller_Fabric::getController('Admin', $this->view, $this->get);
            break;
            default: 
            $actionController = Action_Controller_Fabric::getController('NotFound', $this->view, $this->get);       
            }
    } 

        
          $actionController->run(); 
          $this->view->render();
    }          
}
 
